package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.IBaiVietDAO;
import vn.hocweb.entity.BaiViet;

public class BaiVietDAO extends GenericDAO implements IBaiVietDAO{

	@Override
	public BaiViet findByID(Long id) {
		Connection connection = getConnection();
		BaiViet result = null;
		String sql = "select * from baiviet where mabaiviet = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, id);
			ResultSet effected = pre.executeQuery();
			if (effected.next()){
				long maBaiViet = effected.getLong("mabaiviet");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String noiDung = effected.getString("noidung");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				long maTrang = effected.getLong("matrang");
				
				result = new BaiViet(thuTu, noiBat, hienThi, maBaiViet, nguoiCapNhat, 
						maTrang, ngayCapNhat, ten, moTa, noiDung);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<BaiViet> findAll(int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<BaiViet> result = new ArrayList<BaiViet>();
		String sql = "select * from baiviet limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, firstItem);
			pre.setLong(2, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maBaiViet = effected.getLong("mabaiviet");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String noiDung = effected.getString("noidung");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				long maTrang = effected.getLong("matrang");
				
				BaiViet row = new BaiViet(thuTu, noiBat, hienThi, maBaiViet, nguoiCapNhat, 
						maTrang, ngayCapNhat, ten, moTa, noiDung);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public int size() {
		Connection connection = getConnection();
		int result = 0;
		String sql = "select count(*) from baiviet";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				result = effected.getInt(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(BaiViet entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into baiviet (ten, mota, noidung, ngaycapnhat, " +
				"thutu, noibat, hienthi, nguoicapnhat, matrang) " +
				"values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getTen());
			pre.setString(2, entity.getMoTa());
			pre.setString(3, entity.getNoiDung());
			pre.setDate(4, entity.getNgayCapNhat());
			pre.setInt(5, entity.getThuTu());
			pre.setBoolean(6, entity.isNoiBat());
			pre.setBoolean(7, entity.isHienThi());
			pre.setString(8, entity.getNguoiCapNhat().getTenDangNhap());
			pre.setLong(9, entity.getTrang().getMaTrang());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(BaiViet entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from baiviet where mabaiviet = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, entity.getMaBaiViet());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(BaiViet entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update baiviet set hienthi = ? where mabaiviet = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setLong(2, entity.getMaBaiViet());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(BaiViet entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update baiviet set hienthi = ? where mabaiviet = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setLong(2, entity.getMaBaiViet());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<BaiViet> findAll_danhSachBaiViet(long maT, int firstItem,
			int batchSize) {
		Connection connection = getConnection();
		List<BaiViet> result = new ArrayList<BaiViet>();
		String sql = "select * from baiviet where matrang = ? limit ?, ?";
		PreparedStatement pre;
		try {
			pre = connection.prepareStatement(sql);
			pre.setLong(1, maT);
			pre.setInt(2, firstItem);
			pre.setInt(3, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maBaiViet = effected.getLong("mabaiviet");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String noiDung = effected.getString("noidung");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				long maTrang = effected.getLong("matrang");
				
				BaiViet row = new BaiViet(thuTu, noiBat, hienThi, maBaiViet, nguoiCapNhat, 
						maTrang, ngayCapNhat, ten, moTa, noiDung);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
